<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change" :isEdit="true"/>
    <el-row>
      <el-col :xs="24" :sm="12" :lg="12">
        <div class="layout-all">
        <div class="menu_title" >编辑轮播图：</div>
        <el-form ref="form" :rules="rules" :model="form" label-width="100px" >
          <el-form-item label="轮播图名:" prop="title">
            <el-input v-model="form.title" placeholder="请输入轮播名" style="width:90%" />
          </el-form-item>
          <el-form-item label="轮播位置:" prop="type">
            <el-select v-model="form.type" aria-placeholder="请选择" style="width:90%">
              <el-option
                v-for="item in type_option"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="封面:" prop="thumb">
            <upload-pic :thumb="form.thumb" @update:thumb="form.thumb=$event" width="350" height="150"/>
            <div style="font-size:12px;">建议尺寸：350px X 150px</div> 
          </el-form-item>
          <el-form-item label="url地址:" prop="url">
            <el-input v-model="form.url" placeholder="请输入url地址" style="width:90%" />
          </el-form-item>
          <el-form-item label="排序:" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" type="number" class="form-width-10" />
          </el-form-item>
          <el-form-item label="是否启用:" prop="status">
            <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>
        </el-form>
        </div>
        </el-col>
      <el-col :xs="24" :sm="12" :lg="12">
        <div class="menu_title" >选择跳转地址：</div>
        <url-address @selectMenu="selectMenu" @selectActivity="selectActivity"></url-address>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import urlAddress from '../../../components/url_address/urlAddress'
export default {
  components: { urlAddress },
  name:'BannerForm',
  computed:{
    id(){
      return this.$store.state.user.editId
    }
  },
  data() {
    return {
      is_change: undefined,
      type_option: [{ value: 0, label: '首页' }],
      form: {
        id: 0,
        sort: 0,
        type: 0,
        status: true,
        title: '',
        url: '',
        thumb: ''
      },
      rules: {
        thumb: [
          { required: true, message: '请输入轮播图', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择轮播位置', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.init_into()
  },
  methods: {
    selectMenu (item) {
        this.form.url = item.path
    },
    selectActivity (url) {
      this.form.url = url
    },
    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Banner/save', form, 'POST').then(e => {this.$store.commit('user/SET_EDIT','')})

        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },

    // 获取当前数据
    init_into() {
      this.get('/admin/Banner/info?id=' + this.id, 'POST').then(e => {
        const tpl = e.info.tpl
        if(this.id>0){
          this.form=tpl
           this.form.status =this.form.status== 1
        }
        setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
	      if(this.is_change!=undefined)
        	this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
.menu_title {
  font-size:20px;font-weight:bold;
  padding:10px 0px;
}
.avatar-banner {
  width:120px;
  height:52px;
}
.avatar-banner .el-upload--picture-card {
  width:120px;
  height:52px;
}
.avatar-banner .el-upload--picture-card img {
    width:117px;
  height:50px;
}
</style>
